import Sid from "/src/Test7/SnowflakeID.js";

var DataBlock = [
    {
        id: "058515906580480",
        value: { Height: null, VaporPressure: null },
        option: {
            type: "object",
            properties: {
                Height: { type: "number", description: "数字", format: "double" },
                VaporPressure: { type: "string", description: "字符串", format: "double" },
            },
            additionalProperties: false,
            description: "区域气象环境类",
        },
    },
    { id: "058515910774784", value: "Test", option: { type: "string" } },
    { id: "058515910774785", value: 999, option: { type: "number" } },
    {
        id: "058515910774786",
        value: { Height: null, VaporPressure: null },
        option: {
            type: "object",
            properties: {
                Height: { type: "number", description: "数字", format: "double" },
                VaporPressure: { type: "string", description: "字符串", format: "double" },
            },
            additionalProperties: false,
            description: "对象测试",
        },
    },
];

var LinkBlock = [
    { formId: "058515910774785", toId: "058515906580480", FromKey: null, toKey: "Height" },
    { formId: "058515910774784", toId: "058515906580480", FromKey: null, toKey: "VaporPressure" },
    { formId: "058515906580480", toId: "058515910774786", FromKey: null, toKey: null },
];

var BlockMapping = {};

for (let i = 0; i < DataBlock.length; i++) {
    const element = DataBlock[i];
    BlockMapping[element.id] = element;
    element.From = [];
    element.To = [];
    element.FromStateMapping = {};
    element.StateLength = 0;
}

for (let i = 0; i < LinkBlock.length; i++) {
    const element = LinkBlock[i];
    element.from = BlockMapping[element.formId];
    element.to = BlockMapping[element.toId];
    BlockMapping[element.formId].To.push(element);
    BlockMapping[element.toId].From.push(element);
    BlockMapping[element.toId].FromStateMapping[element.formId] = false;
}

// 取值
function GetValue(Value, Str) {
    var _arr = Str.split(".");
    _arr.unshift(Value);
    return _arr.reduce((p, n) => {
        return p[n];
    });
}

// 赋值
function SetValue(Value, Str, val) {
    var _arr = Str.split(".");
    _arr.unshift(Value);
    return _arr.reduce((p, n, index) => {
        if (index == _arr.length - 1) {
            p[n] = val;
        }
        return p[n];
    });
}

function SendValue(element) {
    var FormValue = element.value;
    for (let i = 0; i < element.To.length; i++) {
        const config = element.To[i];
        const toObj = config.to;
        var _FromV = config.FromKey ? GetValue(FormValue, config.FromKey) : FormValue;
        if (config.toKey) {
            SetValue(toObj.value, config.toKey, _FromV);
        } else {
            toObj.value = _FromV;
        }
        toObj.StateLength++;
        if (toObj.StateLength == toObj.From.length) {
            SendValue(toObj);
        }
    }
}




for (const key in BlockMapping) {
    if (BlockMapping[key].From.length == 0) {
        var element = BlockMapping[key];
        SendValue(element);
    }
}

